package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.NetworkHostDao;
import com.bocloud.cmp.entity.resource.NetworkHost;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;

/**
 * 网络-主机DAO接口实现
 * 
 * @author weiwei
 * @version 1.0
 * @since 2017.4.21
 *
 */
@Repository("networkHostDao")
public class NetworkHostDaoImpl extends BasicDao implements NetworkHostDao {

	@Override
	public NetworkHost save(NetworkHost networkHost) throws Exception {
		return (NetworkHost) this.saveEntity(networkHost);
	}

	@Override
	public boolean update(NetworkHost networkHost) throws Exception {
		return this.updateEntity(networkHost);
	}

	@Override
	public boolean delete(NetworkHost networkHost) throws Exception {
		return this.baseDelete(networkHost);
	}

	@Override
	public List<NetworkHost> list(Long vhostId, String category) throws Exception {
		String sql = "select * from network_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		List<Object> list = this.queryForList(sql, params, NetworkHost.class);
		List<NetworkHost> result = new ArrayList<>();
		for (Object object : list) {
			result.add((NetworkHost) object);
		}
		return result;
	}

	@Override
	public boolean deleteByHost(Long vhostId, String category) throws Exception {
		String sql = "delete from network_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.update(sql, params) > 0;
	}

	@Override
	public boolean deleteByNetwork(Long networkId) throws Exception {
		String sql = "delete from network_host where network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		return this.update(sql, params) > 0;
	}

	@Override
	public boolean removeByVendor(Long vendorId) throws Exception {
		String sql = "delete from network_host where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.update(sql, params) > 0;
	}

}
